/**
 * The MIT License (MIT)
 * Copyright (c) 2021 铭软科技(mingsoft.net)
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:

 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package net.mingsoft.msend.service;

import net.mingsoft.basic.exception.BusinessException;
import net.mingsoft.basic.util.SpringUtil;
import net.mingsoft.basic.util.StringUtil;
import net.mingsoft.mdiy.util.ConfigUtil;
import net.mingsoft.msend.biz.ILogBiz;
import net.mingsoft.msend.entity.LogEntity;
import net.mingsoft.msend.entity.TemplateEntity;
import net.mingsoft.msend.util.SendUtil;
import net.mingsoft.msend.util.SendcloudUtil;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Date;
import java.util.Map;

@Service("sendCouldMailService")
public class SendCouldMailService extends BaseSendService{
    @Override
    public boolean send(Map<String,String> config, String[] toUser, TemplateEntity template, Map values) {
        String mailContent =  this.rendering(values, template.getTemplateMail());

        String _toUser = "";
        for (int i = 0; i < toUser.length; i++) {
            if (StringUtil.isEmail(toUser[i])) {
                _toUser += toUser[i];
                if (i < toUser.length) {
                    _toUser += ";";
                }
            }
        }
        boolean flag = false;
        try {
            flag = SendcloudUtil.sendMail(
                    config.get("mailName"),
                    config.get("mailPassword"),
                    config.get("mailForm"),
                    config.get("mailFormName"),
                    _toUser,template.getTemplateTitle(),
                    mailContent);
        } catch (IOException e) {
            e.printStackTrace();
            throw new BusinessException("Sendcloud 接口调用失败");
        }
        //写入日志
        ILogBiz logBiz = SpringUtil.getBean(ILogBiz.class);
        LogEntity log = new LogEntity();
        if(flag){
            log.setLogType(MAIL);
            log.setTemplateId(Integer.parseInt(template.getId()));
            log.setLogDatetime(new Date());
            log.setLogContent(mailContent);
            log.setLogReceive(_toUser.replace(";",""));
            logBiz.save(log);
        }
        return flag;
    }
}
